package com.dd.web.base.controller;

import com.dd.common.entity.CommonResult;
import com.dd.web.base.entity.common.CommonPage;
import com.dd.web.base.entity.data.UmsMenu;
import com.dd.web.base.entity.view.MenuTreeNode;
import com.dd.web.base.service.UmsMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.DecimalMin;
import java.util.List;

/**
 * 后台菜单管理Controller
 * Created by macro on 2020/2/4.
 */
@Validated
@Slf4j
@RestController
@Api(tags = "UmsMenuController", value = "菜单管理")
@RequestMapping("/base/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService menuService;
//    @Autowired
//    private DynamicSecurityMetadataSource dynamicSecurityMetadataSource;

    @ApiOperation("查询菜单")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<UmsMenu>> listAll(UmsMenu menu) {
        List<UmsMenu> resourceList = menuService.listAll();
        return CommonResult.success(resourceList);
    }

    @ApiOperation(value = "添加后台菜单")
    @PostMapping
    public CommonResult<Integer> create(@RequestBody UmsMenu menu) {
        int count = menuService.create(menu);
//        dynamicSecurityMetadataSource.clearDataSource();
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台菜单")
    @PutMapping
    public CommonResult<Integer> update(@RequestBody UmsMenu menu) {
        int count = menuService.update(menu.getId(), menu);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取菜单详情")
    @GetMapping(value = "/{id}")
    @ApiImplicitParams(@ApiImplicitParam(paramType = "path",name="id",value="资源id",dataType = "Long",required = true))
    public CommonResult<UmsMenu> getItem(@PathVariable("id") Long id) {
        UmsMenu menu = menuService.getItem(id);
        return CommonResult.success(menu);
    }

    @ApiOperation("根据ID删除后台菜单")
    @DeleteMapping(value = "/{id}")
    @ApiImplicitParams(@ApiImplicitParam(paramType = "path",name="id",value="资源id",dataType = "Long",required = true))
    public CommonResult delete(@PathVariable Long id) {
        int count = menuService.delete(id);
//        dynamicSecurityMetadataSource.clearDataSource();
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据父级ID查询子集菜单")
    @GetMapping(value = "/tree/{parentId}")
    @ApiImplicitParams(@ApiImplicitParam(paramType = "path",name="parentId",value="父级id",dataType = "Long",required = true))
    public CommonResult<List<MenuTreeNode>> queryMenuTreeByParentId(@PathVariable("parentId") Long parentId) {
        List<UmsMenu> menuList = menuService.queryMenuByParentId(parentId);
        return CommonResult.success(MenuTreeNode.transTreeNode(menuList));
    }

    /**
     * 分页查询菜单列表
     * @param pageNum 当前页码
     * @param pageSize 每页行数
     * @param menuName 菜单名称
     * @param status 菜单状态
     * @return
     */
    @ApiOperation("分页查询菜单列表")
    @GetMapping("/query")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name="pageNum",required = true,value="当前页码",defaultValue = "1",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name="pageSize",required = true,value="每页行数",defaultValue = "10",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name="menuName",required = false,value="查询条件",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name="status",required = false,value="查询条件",dataType ="int")
            }
    )
    public CommonPage<List<UmsMenu>> queryUmsMenuForPage(@DecimalMin(value = "1",message = "页码要大于1") @RequestParam(value="pageNum",required = true,defaultValue = "1") int pageNum,
                                                         @RequestParam(value = "pageSize",required = true,defaultValue = "10") int pageSize,
                                                         @RequestParam(value = "menuName",required = false)String menuName,
                                                         @RequestParam(value = "status",required = false,defaultValue = "0")int status){
        UmsMenu menu = new UmsMenu();
        menu.setMenuName(menuName);
        menu.setStatus(status);
        return menuService.queryUmsMenuForPage(pageNum,pageSize,menu);
    }


}